package operator.transfer;

import java.util.Arrays;
import java.util.List;
import java.util.concurrent.TimeUnit;

import entity.Like;
import entity.People;
import io.reactivex.Observable;
import io.reactivex.ObservableSource;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import util.DataUtil;
import util.Printer;
import util.ThreadHelper;

class ConcatMapOperator {

    public static void main(String[] args) {
        concatMap();
//        concatMapIterable();
    }

    //有序的
    static void concatMap(){
        Observable.just(Arrays.asList(1, 2, 3, 4, 5), Arrays.asList(6, 7))
                .concatMap(new Function<List<Integer>, ObservableSource<Integer>>() {
                    @Override
                    public ObservableSource<Integer> apply(List<Integer> list) throws Exception {
                        return Observable.fromIterable(list).delay(list.size(), TimeUnit.SECONDS);
                    }
                })
                .subscribe(new Consumer<Integer>() {
                    @Override
                    public void accept(Integer integer) throws Exception {
                        Printer.print(integer.toString());
                    }
                });
        ThreadHelper.sleep();
    }


    static void concatMapIterable() {
        List<People> ps = DataUtil.createPeoples();
        Observable.fromIterable(ps)
                .concatMapIterable(new Function<People, Iterable<Like>>() {
                    @Override
                    public Iterable<Like> apply(People people) throws Exception {
                        return people.likes;
                    }
                })
                .subscribe(new Consumer<Like>() {
                    @Override
                    public void accept(Like like) throws Exception {
                        Printer.print(like.name);
                    }
                });
    }


}
